package com.ruoyi.framework.config;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
public class ApiConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static  final  String jiajia_reids_token = "jiajia:token:";

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> patternsLists = new ArrayList<>();
        patternsLists.add("/api/kehu/user/login");
        patternsLists.add("/api/kehu/user/bindWxmallPhone");
        patternsLists.add("/api/kehu/user/getLoginCode");
        patternsLists.add("/api/kehu/user/loginByJiGuang");
        patternsLists.add("/api/kehu/home/home");
        patternsLists.add("/api/worker/login");
        patternsLists.add("/api/worker/getLoginCode");
        patternsLists.add("/api/common/upload");
        patternsLists.add("/api/common/getCityList");
        patternsLists.add("/api/common/getAddreseByLocation");
        patternsLists.add("/api/kehu/jishi/jiShiList");
        patternsLists.add("/api/kehu/jishi/detail");
        patternsLists.add("/api/xieyi/yulan/**");
        patternsLists.add("/api/xieyi/wtyulan/**");
        patternsLists.add("/api/kehu/version/getVersion");
        patternsLists.add("/api/worker/version/getVersion");
        patternsLists.add("/api/paynotify/**");
        patternsLists.add("/api/kehu/jiajiaCharityActivities/**");
        patternsLists.add("/api/kehu/jiajiaProxyApplication/**");
        patternsLists.add("/api/kehu/JiajiaHomeActivity/**");
        patternsLists.add("/api/kehu/item/**");
        patternsLists.add("/api/common/up/single");
        patternsLists.add("/api/kehu/JiajiaRecruitmentPosters/getInfo");
        patternsLists.add("/api/mall/order/**");
        patternsLists.add("/api/mall/evaluation/**");
        patternsLists.add("/api/mall/level/**");
        patternsLists.add("/api/mall/type/**");
        patternsLists.add("/api/mall/address/**");
        patternsLists.add("/api/mall/shop/**");
        patternsLists.add("/api/mall/car/**");
        patternsLists.add("/api/mall/product/**");
        patternsLists.add("/api/kehu/jiajiaAudit/getInfo");
        patternsLists.add("/api/kehu/jiajia/user/getInfo");
        patternsLists.add("/api/wechat/silentLogin");
        patternsLists.add("/api/wechat/config");
        patternsLists.add("/api/wechat/getCode");
        patternsLists.add("/api/wechat/login");
        patternsLists.add("/api/pay/batchTransfer");


        registry.addInterceptor(getApiAdminInterceptorConfig()).addPathPatterns("/api/**").excludePathPatterns(patternsLists);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

    }
    private static final Logger logger = LoggerFactory.getLogger(ApiConfig.class);

    @Bean
    public ApiAdminInterceptorConfig getApiAdminInterceptorConfig(){
        return new ApiAdminInterceptorConfig();
    }

    class ApiAdminInterceptorConfig implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
            String token = request.getHeader("token");
            //根据token,判断是否登陆
            boolean isLogin = false;
            if (!StringUtils.isEmpty(token)){
                String redisToken = redisTemplate.opsForValue().get(jiajia_reids_token+token);
                if (!StringUtils.isEmpty(redisToken)){
                    isLogin = true;
                }
            }
            if (!isLogin){
                response.setContentType("application/json;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                String res = JSON.toJSONString(AjaxResult.notLogin());
                try {
                    response.getWriter().write(res);
                }catch (Exception e){

                }
                return false;
            }
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            System.out.println("请求地址--"+request.getRequestURI());
            System.out.println("请求方法--"+handler.toString());
            Map<String, String[]> reqParamMap = request.getParameterMap();
            System.out.println("请求参数--"+JSON.toJSONString(reqParamMap));

        }
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        }
    }
}
